package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.IAddressBookService;
import com.sky.context.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user/addressBook")
@Slf4j
public class AddressBookController {
    @Autowired
    private IAddressBookService addressBookService;
    /**
     * 新增地址
     */
    @PostMapping
    public Result save(@RequestBody AddressBook addressBook) {
        log.info("新增地址");
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookService.save(addressBook);
        return Result.success();
    }

    /**
     * 查询当前登录用户的所有地址信息
     */
    @GetMapping("/list")
    public Result list() {
        log.info("查询当前登录用户所有地址信息");
        return Result.success(addressBookService.list());
    }
    /**
     * 查询默认地址
     */
    @GetMapping("/default")
    public Result<AddressBook> getDefault() {
        log.info("查询默认地址");
        return Result.success(addressBookService.getDefault());
    }
    /**
     * 根据ID删除地址
     */
    @DeleteMapping
    public Result deleteById(Long id) {
        log.info("删除地址：{}", id);
        addressBookService.removeById(id);
        return Result.success();
    }

    /**
     * 根据ID查询地址
     */
    @GetMapping("/{id}")
    public Result<AddressBook> getById(@PathVariable Long id) {
        log.info("查询地址：{}", id);
        return Result.success(addressBookService.getById(id));
    }

    /**
     * 修改地址
     */
    @PutMapping
    public Result update(@RequestBody AddressBook addressBook) {
        log.info("修改地址：{}", addressBook);
        addressBookService.updateById(addressBook);
        return Result.success();
    }
    /**
     * 设置默认地址
     */
    @PutMapping("/default")
    public Result setDefault(@RequestBody AddressBook addressBook) {
        log.info("设置默认地址：{}", addressBook);
        addressBookService.setDefault(addressBook);
        return Result.success();
    }
}